<?php
declare (strict_types = 1);

namespace app\admin\middleware;

use think\facade\Session;

class Auth
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
//        dump($request);
        if (empty(Session::get('admin_user')) && !preg_match("/login|captcha/", $request->pathinfo())) {
            return redirect((string)url("/admin/login"));
        }

        if (!empty(Session::get('admin_user')) && preg_match("/login/", $request->pathinfo())) {
            return redirect((string)url("/admin/index"));
        }
        return $next($request);
    }
}
